"""
Copyright (c) 2016 rllab contributors
https://github.com/rll/rllab
"""

import inspect
# import sys

class Serializabe:

    def __init__(self, *args, **kwargs) -> None:
        self.__args = args
        self.__kwargs = kwargs
    
    def quick_init(self, locals_):
        try:
            if object.__getattribute__(self, "_serializable_initialized"):
                return
        except AttributeError:
            pass
        spec = inspect.signature(self.__init__)
        if spec.varkw: # kwargs
            kwargs = locals_[spec.varkw]
        else:
            kwargs = dict()
        if spec.varargs: # 'args'
            varargs = locals_[spec.varargs]
        else:
            varargs = tuple()
        in_order_args = [locals_[arg] for arg in spec.args][1:]
        self.__args = tuple(in_order_args) + varargs
        self.__kwargs = kwargs
        setattr(self, "_serializable_initialized", True)

    def __getstate__(self):
        return {"__args": self.__args, "__kwargs": self.__kwargs}

    def __setstate__(self, d):
        out = type(self)(*d["__args"], **d["__kwargs"])
        self.__dict__.update(out.__dict__)
